package com.luweiteng.valiteddemo.controller;

import com.luweiteng.valiteddemo.entity.BaseResp;
import com.luweiteng.valiteddemo.entity.ValitedBean;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @author ludans
 * @program valited-demo
 * @date 2020/8/8 16:27
 */
@RestController
@RequestMapping(value = "validation")
@Validated
public class ValidatorController {
    @PostMapping(value = "test")
    public BaseResp testValidation(@Valid @RequestBody ValitedBean bean) {
        return BaseResp.success(bean.getAge());
    }

    @GetMapping(value = "testGet")
    public BaseResp testGetValidation(@NotBlank(message = "姓名不能为空！") @RequestParam String name,
                                      @Range(min = 18, max = 66, message = "范围限制在 18-66 ") @RequestParam(required = true) Integer age) {

        return BaseResp.success("成功！");
    }
}
